import 'package:base_flutter/base_flutter.dart';
import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cupertino_date_picker_fork/flutter_cupertino_date_picker_fork.dart' as dt;

import '../../a_yyt/models/pending_entity.dart';
import '../../res/colors.dart';
import '../../res/gaps.dart';
import '../../res/styles.dart';
import '../../util/flat_button.dart';
import '../../widgets/add_image_widget.dart';
import '../../widgets/click_item.dart';

class ServiceExcuteSubmitWidget extends StatefulWidget {
  PendingModel model;
  Function(PendingModel model)? delayClick;
  Function(PendingModel model, bool isResultMode,List<String> photoIds,String voiceId,String selectTime)? submitClick;

  ServiceExcuteSubmitWidget(this.model, {this.delayClick, this.submitClick});


  @override
  State<StatefulWidget> createState() => _ServiceExcuteSubmitState();


}

class _ServiceExcuteSubmitState extends State<ServiceExcuteSubmitWidget>{

  late PendingModel model;
  TextEditingController textViewController = TextEditingController();

  @override
  void initState() {
    model = widget.model;
    super.initState();
  }
  var files = <String>[];
  var voiceFileId = "";
  var _selectedTime = DateTime.now().toDateStr(
      ['yyyy', '-', 'mm', '-', 'dd', ' ', 'HH', ':', 'nn', ':', 'ss']);

  @override
  Widget build(BuildContext context) {

    return Stack(
      children: <Widget>[
        SingleChildScrollView(
          child: Container(
            width: double.infinity,
            padding: EdgeInsets.only(
                bottom: MediaQuery.of(context).viewInsets.bottom), //
            child: Column(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Gaps.vGap50,
                Text(
                  '${widget.model.expenseItem}',
                  style: TextStyle(fontSize: 20, color: Colours.text_dark),
                ),
                Gaps.vGap15,
                Text(
                  '${widget.model.elderName}   ${widget.model.bedNo}',
                  style: TextStyles.textGray18,
                ),
                Gaps.vGap50,
                ClickItem(
                  title: "执行时间：",
                  content: _selectedTime,
                  onTap: () {
                    _showDateTimePicker(context, calBack: (type, dateTime) {
                      setState(() {
                        _selectedTime = dateTime;
                      });
                    });
                  },
                ),
                Gaps.vGap15,
                Container(
                  padding: EdgeInsets.all(20),
                  color: Colors.white,
                  alignment: Alignment.topLeft,
                  height: 100,
                  child: TextField(
                    controller: textViewController,
                    maxLines: 5,
                    // maxLength: 300,
                    autofocus: false,
                    decoration: InputDecoration(
                      hintText: "请输入备注内容",
                      hintMaxLines: 20,
                      fillColor: Colours.bg_color,
                      filled: true,
                      border: InputBorder.none,
                    ),
                  ),
                ),
                // UploadVoiceWidget((model) {
                //   voiceFileId = model.id.toString();
                // }).addToContainer(
                //     margin: EdgeInsets.symmetric(horizontal: 16)),
                AddAndUploadImageWidget(
                      (file, modelList) {
                    files = file;
                  },
                  files: files,
                  max: 3,
                  cameramsg: "拍摄照护执行照片，需要申请您的相机权限",
                  photomsg: "从相册选取照护执行照片，需要申请您的相册权限",
                ).addToContainer(margin: EdgeInsets.symmetric(horizontal: 8)),
                Gaps.vGap50,
                Container(
                  width: double.infinity,
                  height: 50,
                  padding: EdgeInsets.fromLTRB(12, 0, 12, 0),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: <Widget>[
                      FlatsButton(
                        color: Colours.text_gray,
                        onPressed: () {
                          Navigator.pop(context);
                        },
                        child: Text(
                          '取消',
                          style: TextStyle(color: Colors.white),
                        ),
                      ),
                      FlatsButton(
                        color: Colours.app_main,
                        onPressed: () {
                          // _updateServicePlanRecord(model: model);
                          // _updateServicePlanRecord(isResultMode: true);
                          if (widget.submitClick != null) {
                            widget.submitClick!(model, true,files,voiceFileId,_selectedTime);
                          }
                        },
                        child: Text(
                          '确定',
                          style: TextStyle(color: Colors.white),
                        ),
                      ),
                    ],
                  ),
                ),
                Gaps.vGap50,
              ],
            ),
          ),
        ),
        Positioned(
          left: 20,
          top: 5,
          child: FlatsButton(
            color: Colors.blue[300],
            onPressed: () {
              Navigator.pop(context);
              // _bottomDelayServiceSheetAlert(
              //     model: model, isDoneItem: true);
              if (widget.delayClick != null) {
                widget. delayClick!(model);
              }
            },
            child: Text(
              '延迟执行',
              style: TextStyle(color: Colors.white),
            ),
          ),
        ),
      ],
    );
  }

  void _showDateTimePicker(BuildContext context,
      {int timeType = 0,
        DateTime? initialDateTime,
        Function(int type, String dateTime)? calBack}) {
    dt.DatePicker.showDatePicker(
      context,
      minDateTime: DateTime.parse("1999-01-01"),
      maxDateTime: DateTime.parse("2099-01-01"),
      initialDateTime: initialDateTime,
      dateFormat: timeType == 1 ? DateFormats.y_mo_d : "yyyy-MM-dd EEE,H时:m分",
      locale: dt.DateTimePickerLocale.zh_cn,
      pickerTheme: dt.DateTimePickerTheme(
        showTitle: true,
        cancel: Text("取消"),
        confirm: Text('确定'),
      ),
      pickerMode:
      timeType == 1 ? dt.DateTimePickerMode.date : dt.DateTimePickerMode.datetime,
      // show TimePicker
      onConfirm: (dateTime, List<int> index) {
        if (timeType == 1) {
          var _todoTime =
          DateUtil.formatDate(dateTime, format: DateFormats.y_mo_d);
          if(calBack!=null){
            calBack(timeType, _todoTime);
          }

        } else {
          var time =
          DateUtil.formatDate(dateTime, format: 'yyyy-MM-dd HH:mm:ss');
          if(calBack!=null){
            calBack(timeType, time);
          }

        }
      },
    );
  }

}
